package com.landmark;

import java.io.*;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.SimpleHtmlSerializer;
import org.htmlcleaner.TagNode;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * Example program to list links from a URL.
 */
public class ListAllUrls {
    
	public void start(String[] args) throws IOException {
		//Validate.isTrue(args.length == 1, "usage: supply url to fetch");
		String url = "http://www.holsters.com/";
		ProcessUrl(url);
	}

	/**
	 * @param aUrl
	 * @throws IOException
	 * This method will get the pages from the given URL and we are neglecting the URL with PDF extension
	 * We have to fetch data from the page with Model*.asp 
	 */
	private  void ProcessUrl(String aUrl) {
		//print("Fetching %s...", aUrl);
		int urlDotLastIndex = aUrl.lastIndexOf('.');
		int urlLength = aUrl.length();
		int urlSlashLastIndex = aUrl.lastIndexOf('/');

		if( !aUrl.substring(urlDotLastIndex+1, urlLength).equalsIgnoreCase("PDF")) {			

			String styleClassName = "category_menu"; // this will get from the user input
			Elements elements = null;
			int urlSlashCount = aUrl.split("/").length;
			Document doc = null;

			if( aUrl.substring(urlDotLastIndex+1, urlLength).equalsIgnoreCase("ASP") && aUrl.substring(urlSlashLastIndex+1, urlLength).contains("model") ) {
				System.out.println(aUrl);
				try{
					doc = Jsoup.connect(aUrl).get();
				}catch (Exception e) {
					e.printStackTrace();					
				}
				String data = doc.html();// extracts the HTML data from the Document doc
				System.out.println(data);
				//htmlToXML(data);
			}else {
				try{
					doc = Jsoup.connect(aUrl).get();
				}catch (Exception e) {
					e.printStackTrace();
				}
				//doc.
				if( urlSlashCount <= 3 ) {
					elements = doc.getElementsByClass(styleClassName); 
				} else  {
					elements = doc.select("a[href]");
				}

				Elements eleLinks = elements.select("a[href]");

				//print("\nLinks: (%d)", eleLinks.size());
				DisplayLinks(eleLinks, aUrl);
			}

		} /*else if( aUrl.substring(urlDotLastIndex+1, urlLength).equalsIgnoreCase("ASP") && aUrl.substring(urlSlashLastIndex+1, urlLength).contains("model") ) {
			System.out.println(aUrl);
		}*/

	}

	private  void htmlToXML(String htmlString)  {
		final CleanerProperties props = new CleanerProperties();
		final HtmlCleaner htmlCleaner = new HtmlCleaner(props);
		final SimpleHtmlSerializer htmlSerializer = new SimpleHtmlSerializer(props);
		TagNode tagNode =  htmlCleaner.clean(htmlString);// Will format to a wellformed XML format
		System.out.println(tagNode);
		try{
			htmlSerializer.writeToFile(tagNode, "output.xml");
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*private  void PrintLinks(Elements aEleLinks, String aUrl) throws IOException {
		int urlSlashCount = aUrl.split("/").length;
		for (Element link : aEleLinks) {
			String actualUrl = link.attr("abs:href");        	

			int actualUrlSlashCount = actualUrl.split("/").length;
			if( actualUrl.contains(aUrl) && actualUrl.length()>aUrl.length() && actualUrlSlashCount == (urlSlashCount+1)) {
				print("<%s> (%s)", link.attr("abs:href"), trim(link.text(), 35));
				ProcessUrl(actualUrl);
			} 
			// print("<%s> (%s)", link.attr("abs:href"), trim(link.text(), 35));
		}
	}*/

	private  void DisplayLinks(Elements aEleLinks, String aUrl)  {
		int urlSlashCount = aUrl.split("/").length;
		for (Element link : aEleLinks) {
			String actualUrl = link.attr("abs:href");        	
			//System.out.println(actualUrl);

			int actualUrlSlashCount = actualUrl.split("/").length;
			//System.out.println(actualUrl.contains(aUrl) +","+actualUrl.length()+","+aUrl.length()+  ", " + actualUrlSlashCount +", "+(urlSlashCount+1));
			if( actualUrl.contains(aUrl) && actualUrl.length()>aUrl.length() && actualUrlSlashCount >= (urlSlashCount+1)) {
				//print("<%s> (%s)", link.attr("abs:href"), trim(link.text(), 35));
				ProcessUrl(actualUrl);
			} 
			// print("<%s> (%s)", link.attr("abs:href"), trim(link.text(), 35));
		}
	}

	private  void print(String msg, Object... args) {
		System.out.println(String.format(msg, args));
	}

	private  String trim(String s, int width) {
		if (s.length() > width)
			return s.substring(0, width-1) + ".";
		else
			return s;
	}
     
}